﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//日志
namespace help
{
    public class LogType
    {
        public static int InfoType = 1;
        public static int ErrorType = 2;
        public static int ExceptionType = 3;
    }

    public class Log
    {
        public event Action<string, int> OnMessageAdd; //增加一条日志

        //写一行
        public void writeLine(string message, int logtype)
        {
            if (!string.IsNullOrEmpty(message))
            {
                message = string.Format("{0:yyyy-MM-dd HH:mm:ss.fff} - {1}", TimerHelp.Now, message);
            }

            Console.WriteLine(message);
            if (OnMessageAdd != null)
            {
                OnMessageAdd.Invoke(message, logtype);
            }
        }

        public void info(string message)
        {
            writeLine(message,LogType.InfoType);
        }

        public void info(string format, params object[] args)
        {
            writeLine(string.Format(format, args), LogType.InfoType);
        }

        //异常
        public void LogException(string exception, string message = "Exception")
        {
            string text=string.Format("{0}:{1}{2}", message, Environment.NewLine, exception);
            writeLine(text, LogType.ExceptionType);
        }

        public void LogException(Exception exception, string message = "Exception")
        {
            LogException(exception.ToString(), message);
        }

        //错误
        public void Error(string format, params object[] args)
        {
            writeLine(string.Format(format, args), LogType.ErrorType);
        }

        public void Error(string message)
        {
            writeLine(message, LogType.ErrorType);
        }
        

    }
}
